package pearson.automation.forms;

import junit.framework.Assert;

import org.openqa.selenium.By;

import pearson.automation.forms.registration.ForgotPasswordForm;

import framework.BaseForm;
import framework.Browser;
import framework.elements.Button;
import framework.elements.Link;
import framework.elements.TextBox;

public class LoginForm extends BaseForm {

    private static Button submit = new Button(By
            .xpath("//input[@value='Sign in']"), "Login");
    private TextBox login = new TextBox(By.id("username"), "Login");
    private TextBox password = new TextBox(By.id("password"), "Password");
    private Link forgotPassword = new Link(By.className("forgottenPassword"), "Forgot password");
    private final String forgotPasswordWindowURL = Browser.props.getProperty("forgotPasswURL");
    
    public LoginForm() {
        super(submit.getLocator(), submit.getName());
    }

    public BrowseForm login(String userName, String passwordValue) {
        login.type(userName);
        password.type(passwordValue);
        submit.clickAndWait();
        isLogged = true;
        return new BrowseForm();
    }

    public WrongPasswordForm loginWrong(String userName, String wrongPassword) {
        login.type(userName);
        password.type(wrongPassword);
        submit.clickAndWait();
        return new WrongPasswordForm();
    }
    
    public ForgotPasswordForm clickForgotPassword() {
        forgotPassword.clickAndWaitForNewWindow();
        
        //Verify that correct window is opened
        Assert.assertEquals(forgotPasswordWindowURL, browser.getDriver().getCurrentUrl());
        
        return new ForgotPasswordForm();
    }
}
